package com.jy.blog.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jy.blog.model.User;
import com.jy.blog.service.UserService;
import com.jy.blog.service.impl.UserServiceImpl;

/**
 * Servlet implementation class UserServlet
 */
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserService service=new UserServiceImpl(); 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
			
		}
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = new User();
		user.setUserName(request.getParameter("userName"));
		user.setPassword(request.getParameter("password"));
		user.setLevel(Integer.valueOf(request.getParameter("level")));
		String vCode = request.getParameter("vCode");
		HttpSession session = request.getSession();
		String svCode = (String)session.getAttribute("svCode");
		if(service.login(user).getUserName()!=null && user.getLevel()==1 && vCode.equalsIgnoreCase(svCode)){	
			session.setAttribute("userName",user.getUserName());
			response.sendRedirect("admin/index.jsp");
		}else if(service.login(user).getUserName()!=null && user.getLevel()==0 && vCode.equalsIgnoreCase(svCode)){			
			session.setAttribute("userName",user.getUserName());
			response.sendRedirect("blog.jsp");
		}else{
			response.sendRedirect("login.jsp");
		}	
	}
	}


